package com.ninetaildemonfox.zdl.socialproject.mvp.ui.activity;

import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ninetaildemonfox.zdl.socialproject.base.BaseActivity;
import com.ninetaildemonfox.zdl.socialproject.R;
import com.ninetaildemonfox.zdl.socialproject.data.entity.VideoInfo;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.adapter.SelectorVideoAdapter;
import com.ninetaildemonfox.zdl.socialproject.mvp.ui.adapter.viewholder.BaseAdapterRV;
import com.ninetaildemonfox.zdl.socialproject.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * @author NineTailDemonFox
 * @date 2019/8/25 15:29
 * 功能描述： 视频上传
 * 联系方式：1037438704@qq.com
 */
public class VideoActivity extends BaseActivity {
    private RecyclerView mRecyclerView;
    private List<VideoInfo> mVideoInfoList = new ArrayList<>();
    private SelectorVideoAdapter mAdapter;
    private VideoInfo mVideoInfo;

    @BindView(R.id.image_left_finish)
    ImageView imageleftFinish;
    @BindView(R.id.text_center)
    TextView textCenter;
    @BindView(R.id.text_right)
    TextView textRight;


    @Override
    protected int getLayoutResource() {
        return R.layout.activity_video;
    }

    @Override
    protected void onInitialization(Bundle bundle) {
        mRecyclerView = (RecyclerView) findViewById(R.id.rv_selector_video);
        textRight.setVisibility(View.VISIBLE);
        textRight.setText("确定");
        textCenter.setText("视频选择");
        imageleftFinish.setVisibility(View.VISIBLE);
        initView();
        getVideoInfoList();
    }

    private void initView() {
        GridLayoutManager linearLayoutManager = new GridLayoutManager(me, 3);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        mAdapter = new SelectorVideoAdapter();
        mRecyclerView.setAdapter(mAdapter);

        mAdapter.setItemOnClickListener(new BaseAdapterRV.OnItemClickListener() {
            @Override
            public void onItemClick(int position, View view, Object o) {
                VideoInfo info = (VideoInfo) o;
                if (info.getDuration() > 10000) {
                    ToastUtil.show("选择的视频不能大于10秒");
                    return;
                }
                mAdapter.setSelectorPosition(position);
                if (mAdapter.getSelectorPosition() != -1) {
                    mVideoInfo = info;
                } else {
                    mVideoInfo = null;
                }
            }
        });

    }

    @OnClick({R.id.image_left_finish, R.id.text_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.image_left_finish:
                finish();
                break;
                case R.id.text_right:
                if (mVideoInfo == null) {
                    Toast.makeText(me, "请您选择视频", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    PictureUploadActivity.start(me, mVideoInfo.getPath(), "1", "1");
                    finish();
                }
                break;
            default:
        }
    }


    /**
     * 获取本地视频资源是耗时操作！！！！可以不使用rxjava 线程中操作也可以
     */
    private void getVideoInfoList() {
        Cursor cursor = getContentResolver().query(
                MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null,
                null, null);

        if (cursor == null) {
            return;
        }
        Observable.just(cursor)
                .map(new Func1<Cursor, List<VideoInfo>>() {
                    @Override
                    public List<VideoInfo> call(Cursor cursor) {
                        return cursorToList(cursor);
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<List<VideoInfo>>() {
                    @Override
                    public void call(List<VideoInfo> videoInfos) {
                        mAdapter.addData(videoInfos);

                        mAdapter.notifyDataSetChanged();

                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        throwable.printStackTrace();
                    }
                });
    }

    /**
     * 将扫描的视频添加到集合中
     *
     * @param cursor
     * @return
     */
    private List<VideoInfo> cursorToList(Cursor cursor) {

        mVideoInfoList.clear();
        VideoInfo videoInfo;
        while (cursor.moveToNext()) {
            int id = cursor.getInt(cursor
                    .getColumnIndexOrThrow(MediaStore.Video.Media._ID));
            String title = cursor
                    .getString(cursor
                            .getColumnIndexOrThrow(MediaStore.Video.Media.TITLE));
            String album = cursor
                    .getString(cursor
                            .getColumnIndexOrThrow(MediaStore.Video.Media.ALBUM));

            String artist = cursor
                    .getString(cursor
                            .getColumnIndexOrThrow(MediaStore.Video.Media.ARTIST));
            String displayName = cursor
                    .getString(cursor
                            .getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME));
            String mimeType = cursor
                    .getString(cursor
                            .getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE));
            String path = cursor
                    .getString(cursor
                            .getColumnIndexOrThrow(MediaStore.Video.Media.DATA));
            long duration = cursor
                    .getInt(cursor
                            .getColumnIndexOrThrow(MediaStore.Video.Media.DURATION));
            long size = cursor
                    .getLong(cursor
                            .getColumnIndexOrThrow(MediaStore.Video.Media.SIZE));

            videoInfo = new VideoInfo(id, title, album, artist, displayName,
                    mimeType, path, size, duration);
            mVideoInfoList.add(videoInfo);
        }
        cursor.close();

        return mVideoInfoList;
    }
}
